package jp.co.heartsoft.gpssearch;

import android.app.Activity;
import android.app.ProgressDialog;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

public class GpsSearchActivity extends Activity implements LocationListener{

	View view;

	ProgressDialog progress;

	LocationManager locManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    }

    @Override
    protected void onResume() {
    	progress = new ProgressDialog(this);
    	progress.setMessage(getText(R.string.progress_location));
    	progress.show();

    	locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

    	super.onResume();
    }

    @Override
    protected void onPause() {
    	super.onPause();
    }

	@Override
	public void onLocationChanged(Location location) {
		locManager.removeUpdates(this);

		new GeocodingTask().execute(location);
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO 自動生成されたメソッド・スタブ

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO 自動生成されたメソッド・スタブ

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO 自動生成されたメソッド・スタブ

	}

	/**
	 * Google Geocoding API タスククラス。
	 */
	private class GeocodingTask extends AsyncTask<Location, Void, String>{

		@Override
		protected String doInBackground(Location... params) {
			// TODO ここでGoogle APIを使用
			return params[0].getLatitude() + " " + params[0].getLongitude() + "(" + params[0].getAccuracy() + ")";
		}

		@Override
		protected void onPostExecute(String result) {
			if (null != progress){
				progress.dismiss();
			}

			// TODO ここでGoogle APIの結果を描画
			((TextView)findViewById(R.id.textView1)).setText(result);
		}
	}
}